package tsp.cn.http;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;
import org.xutils.common.Callback;
import tsp.cn.http.interfac.INetResult;
import tsp.cn.http.tools.LogUtil;
import tsp.cn.http.tools.NetWorkUtil;
import android.content.Context;
import org.xutils.http.RequestParams;

/**
 * 请求基类 Created by wds on 2016/10/17.
 */
public abstract class BaseIDao {
	private INetResult mINetResult;
	private Context mContext;

	public BaseIDao(Context context, INetResult iNetResult) {
		this.mContext = context;
		this.mINetResult = iNetResult;
	}

	/**
	 * 得到结果后，对结果处理逻辑
	 * 
	 * @param result
	 * @param requestCode
	 * @throws java.io.IOException
	 */
	public abstract void onRequestSuccess(String result, int requestCode);

	/**
	 * get请求网络，本方法提供结果的分发
	 * 
	 * @param url
	 * @param requestCode
	 */
	public void getRequest(String url, final int requestCode) {
		// 判断网络
		if (!NetWorkUtil.CheckNet(mContext)) {
			mINetResult.onNoConnect();
			return;
		}
		LogUtil.v("URL：" + url);
		x.http().get(new RequestParams(url),
				new Callback.CacheCallback<String>() {
					@Override
					public boolean onCache(String o) {
						return false;
					}

					@Override
					public void onSuccess(String result) {
						LogUtil.v("返回数据：" + result);
						String respStatus = null;
						try {
							JSONObject jsonObject = new JSONObject(result);
							respStatus = jsonObject.get("resp_status")
									.toString();
						} catch (JSONException e) {
							e.printStackTrace();
						}
						if ("OK".equals(respStatus))
							onRequestSuccess(result, requestCode);

						mINetResult.onRequestSuccess(requestCode);
					}

					@Override
					public void onError(Throwable e, boolean b) {
						mINetResult.onRequestFailed(e.getMessage(),
								e.getMessage());
						LogUtil.v("错误信息：" + e.getMessage());
					}

					@Override
					public void onCancelled(CancelledException e) {

					}

					@Override
					public void onFinished() {

					}
				});

	}

	/**
	 * post 请求网络，本地方法提供结果的分发
	 * 
	 * @param url
	 * @param params
	 * @param requestCode
	 */
	public void postRequest(RequestParams params, final int requestCode) {
		// 判断网络
		if (!NetWorkUtil.CheckNet(mContext)) {
			mINetResult.onNoConnect();
			return;
		}
		LogUtil.v("URL：" + params.getUri());
		x.http().get(params, new Callback.CacheCallback<String>() {
			@Override
			public boolean onCache(String o) {
				return false;
			}

			@Override
			public void onSuccess(String result) {
				LogUtil.v("返回数据：" + result);
				String respStatus = null;
				try {
					JSONObject jsonObject = new JSONObject(result);
					respStatus = jsonObject.get("resp_status").toString();
				} catch (JSONException e) {
					e.printStackTrace();
				}
				if ("OK".equals(respStatus))
					onRequestSuccess(respStatus, requestCode);

				mINetResult.onRequestSuccess(requestCode);
			}

			@Override
			public void onError(Throwable e, boolean b) {
				mINetResult.onRequestFailed(e.getMessage(), e.getMessage());
				LogUtil.v("错误信息：" + e.getMessage());
			}

			@Override
			public void onCancelled(CancelledException e) {

			}

			@Override
			public void onFinished() {

			}
		});
	}
}